package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.entity.vo.*;
import com.jrtc.base.util.MinioUtil;
import com.jrtc.dao.*;
import com.jrtc.service.ExerciseprescriptionService;
import com.jrtc.service.PrescriptionLabelOneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.List;

/**
 * 运动处方表(Exerciseprescription)表服务实现类
 *
 * @author makejava
 * @since 2022-03-21 17:34:12
 */
@Transactional
@Service("exerciseprescriptionService")
public class ExerciseprescriptionServiceImpl implements ExerciseprescriptionService
{
    @Resource
    private ExerciseprescriptionDao exerciseprescriptionDao;
    @Resource
    private MinioUtil minioUtil;
    @Resource
    private DiseaseDao diseaseDao;
    @Resource
    private PublicVideoDao publicVideoDao;
    @Resource
    private PatientExerciseprescriptionDao patientExerciseprescriptionDao;
    @Resource
    private DoctorDao doctorDao;
    @Resource
    private DiseasecategoryDao diseasecategoryDao;
    @Resource
    private HealthplanDao healthplanDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private ExerciserecordDao exerciserecordDao;
    @Resource
    private PrescriptionLabelOneDao prescriptionLabelOneDao;
    @Resource
    private PrescriptionLabelTwoDao prescriptionLabelTwoDao;
    @Resource
    private PrescriptionLabelThreeDao prescriptionLabelThreeDao;
    @Resource
    private SysDictDao sysDictDao;
    @Resource
    private SysDictItemDao sysDictItemDao;
    @Resource
    private PrescriptionLabelOneService prescriptionLabelOneService;

    /**
     * 通过ID查询单条数据
     *
     * @param prescriptionId 主键
     * @return 实例对象
     */
    @Override
    public Exerciseprescription queryById(Integer prescriptionId) {
        return this.exerciseprescriptionDao.queryById(prescriptionId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Exerciseprescription> queryAllByLimit(int offset, int limit) {
        return this.exerciseprescriptionDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<Exerciseprescription> queryExerciseprescriptionList(Exerciseprescription exerciseprescription)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Exerciseprescription> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(exerciseprescription.getPageNo(), exerciseprescription.getPageSize());
        IPage<Exerciseprescription> exerciseprescriptionPage = this.exerciseprescriptionDao.queryExerciseprescriptionList(page, exerciseprescription);
        return exerciseprescriptionPage;
    }

    /**
     * 新增数据
     *
     * @param exerciseprescription 实例对象
     * @return 实例对象
     */
    @Override
    public Exerciseprescription insert(Exerciseprescription exerciseprescription) {
        this.exerciseprescriptionDao.insert(exerciseprescription);
        return exerciseprescription;
    }

    /**
     * 修改数据
     *
     * @param exerciseprescription 实例对象
     * @return 实例对象
     */
    @Override
    public Exerciseprescription update(Exerciseprescription exerciseprescription) {
        this.exerciseprescriptionDao.update(exerciseprescription);
        return this.queryById(exerciseprescription.getPrescriptionId());
    }

    /**
     * 通过主键删除数据
     *
     * @param prescriptionId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer prescriptionId) {
        return this.exerciseprescriptionDao.deleteById(prescriptionId) > 0;
    }

    /**
     * 查询指定运动处方详细信息
     * */
    @Override
    public Exerciseprescription prescribingDetails(Integer prescriptionId, String prescriptionDesc) throws Exception {
        if (prescriptionId != null && prescriptionId != 0){
            Exerciseprescription exerciseprescription = exerciseprescriptionDao.selectExerciseprescription(prescriptionId, prescriptionDesc);
            if (exerciseprescription != null){
                Disease disease = diseaseDao.queryById(exerciseprescription.getDiseaseId());
                if (disease != null){
                    exerciseprescription.setDiseaseName(disease.getDiseaseName());

                    Diseasecategory diseasecategory = diseasecategoryDao.queryById(disease.getDiseasegategoryId());
                    if (diseasecategory != null){
                        exerciseprescription.setDiseaseClassification(diseasecategory.getDiseasecategoryName());
                        exerciseprescription.setDiseaseClassificationId(diseasecategory.getDiseasecategoryId());
                    }
                }

                List<PublicVideo> publicVideos = publicVideoDao.selectPrescriptionvideo(exerciseprescription.getPrescriptionId());
                exerciseprescription.setPrescriptionVideo(publicVideos);

                return exerciseprescription;
            }
            else {
                throw new Exception("未查到运动处方详细信息");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 修改运动处方信息
     * */
    @Override
    public int modifyInformation(Exerciseprescription exerciseprescription) throws Exception {
        if (exerciseprescription != null){
            int i = exerciseprescriptionDao.updateExerciseprescription(exerciseprescription);
            if (i > 0){
                if (exerciseprescription.getPrescriptionVideo() != null && exerciseprescription.getPrescriptionVideo().size() > 0){
                    for (PublicVideo publicVideo : exerciseprescription.getPrescriptionVideo()) {
                        publicVideo.setPrescriptionId(exerciseprescription.getPrescriptionId());
                        if (publicVideo.getPrescriptionVideoId() != null){
                            publicVideoDao.updatePrescriptionvideo(publicVideo);
                        }
                        else {
                            publicVideoDao.insertPrescriptionvideo(publicVideo);
                        }
                    }
                }

                return i;
            }
            else {
                throw new Exception("修改失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 创建运动处方
     * */
    @Override
    public int insertExerciseprescription(Exerciseprescription exerciseprescription, Doctor loginDoctor) throws Exception {
        if (exerciseprescription != null){
            exerciseprescription.setDoctorId(loginDoctor.getDoctorId());
//            exerciseprescription.setDoctorId(1);

            String classificationId = "";
            if (exerciseprescription.getClassificationIdList() != null && exerciseprescription.getClassificationIdList().size() > 0){
                for (int i = 0; i < exerciseprescription.getClassificationIdList().size(); i++) {
                    if (i == 0){
                        classificationId = exerciseprescription.getClassificationIdList().get(i).toString();
                    }
                    else {
                        classificationId = classificationId + "," + exerciseprescription.getClassificationIdList().get(i).toString();
                    }
                }
            }

            exerciseprescription.setClassificationId(classificationId);

            int i;
            //修改
            if (exerciseprescription.getPrescriptionId() != null && exerciseprescription.getPrescriptionId() != 0){
                i = exerciseprescriptionDao.updateExerciseprescription(exerciseprescription);
            }
            //新增
            else {
                i = exerciseprescriptionDao.insertExerciseprescription(exerciseprescription);
            }

            if (i <= 0){
                throw new Exception("添加运动处方失败");
            }

            //修改
            if (exerciseprescription.getPrescriptionId() != null && exerciseprescription.getPrescriptionId() != 0)
            {
//                List<PrescriptionLabelOne> prescriptionLabelOnes = prescriptionLabelOneDao.prescriptionLabelOne(exerciseprescription.getPrescriptionId());
                List<PrescriptionLabelOne> prescriptionLabelOnes = exerciseprescription.getLstLabel();
                List<Integer> lstOne = new ArrayList<>();
                List<Integer> lstTwo = new ArrayList<>();
                List<Integer> lstThree = new ArrayList<>();
                if (prescriptionLabelOnes != null && prescriptionLabelOnes.size() > 0)
                {
                    for (PrescriptionLabelOne prescriptionLabelOne : prescriptionLabelOnes)
                    {
                        lstOne.add(prescriptionLabelOne.getId());
                        List<PrescriptionLabelTwo> prescriptionLabelTwos = prescriptionLabelOne.getLabelTwos();
                        if (prescriptionLabelTwos != null && prescriptionLabelTwos.size() > 0)
                        {
                            for (PrescriptionLabelTwo prescriptionLabelTwo : prescriptionLabelTwos)
                            {
                                lstTwo.add(prescriptionLabelTwo.getId());
                                List<PrescriptionLabelThree> prescriptionLabelThrees = prescriptionLabelTwo.getLabelThrees();
                                if (prescriptionLabelThrees != null && prescriptionLabelThrees.size() > 0)
                                {
                                    for (PrescriptionLabelThree prescriptionLabelThree : prescriptionLabelThrees)
                                    {
                                        lstThree.add(prescriptionLabelThree.getId());
                                    }
                                }
                            }
                        }
                    }
                }
                for (Integer one : lstOne)
                {
                    prescriptionLabelOneDao.deleteById(one);
                }
                for (Integer two : lstTwo)
                {
                    prescriptionLabelTwoDao.deleteById(two);
                }
                for (Integer three: lstThree)
                {
                    prescriptionLabelThreeDao.deleteById(three);
                }
                prescriptionLabelOneService.insertPrescriptionLabel(exerciseprescription.getLstLabel());
            }
            //新增
            else {
                if (exerciseprescription.getPrescriptionVideo() != null && exerciseprescription.getPrescriptionVideo().size() > 0){
                    for (PublicVideo prescriptionVideo : exerciseprescription.getPrescriptionVideo()) {
                        PublicVideo publicVideo = new PublicVideo();
                        publicVideo.setPrescriptionVideoUrl(prescriptionVideo.getPrescriptionVideoUrl());
                        prescriptionVideo.setPrescriptionVideoName(prescriptionVideo.getPrescriptionVideoName());
                        publicVideo.setPrescriptionId(exerciseprescription.getPrescriptionId());
                        int insertPrescriptionvideo = publicVideoDao.insertPrescriptionvideo(publicVideo);
                        if (insertPrescriptionvideo <= 0){
                            throw new Exception("视频添加失败" + prescriptionVideo);
                        }
                    }
                }
            }

            if (exerciseprescription.getLstLabel() != null && exerciseprescription.getLstLabel().size() > 0){
                for (PrescriptionLabelOne prescriptionLabelOne : exerciseprescription.getLstLabel()) {
                    prescriptionLabelOne.setExercisePrescriptionId(exerciseprescription.getPrescriptionId());

                    int one = prescriptionLabelOneDao.insert(prescriptionLabelOne);
                    if (one <= 0){
                        throw new Exception("添加运动处方标签" + prescriptionLabelOne.getName() + "时出错");
                    }

                    if (prescriptionLabelOne.getLabelTwos() != null && prescriptionLabelOne.getLabelTwos().size() > 0){
                        for (PrescriptionLabelTwo labelTwo : prescriptionLabelOne.getLabelTwos()) {
                            labelTwo.setLabelOneId(prescriptionLabelOne.getId());

                            int two = prescriptionLabelTwoDao.insert(labelTwo);
                            if (two <= 0){
                                throw new Exception("添加运动处方标签" + labelTwo.getName() + "时出错");
                            }

                            if (labelTwo.getLabelThrees() != null && labelTwo.getLabelThrees().size() > 0){
                                for (PrescriptionLabelThree labelThree : labelTwo.getLabelThrees()) {
                                    labelThree.setLabelTwoId(labelTwo.getId());

                                    int three = prescriptionLabelThreeDao.insert(labelThree);
                                    if (three <= 0){
                                        throw new Exception("添加运动处方标签" + labelThree.getName() + "时出错");
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (i > 0){
                return i;
            }
            else {
                throw new Exception("创建失败");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 上传运动处方文件/视频
     * */
    @Override
    public List<String> uploadFile(List<MultipartFile> fileList, String type) throws Exception {
        if (fileList.size() > 0){
            String source = "prescription";
            String business;

            //运动处方文件
            if (type.equals(PublicConstants.UniversalEnum.Document.getValue())){
                business = "document";
            }
            //医生头像
            else if (type.equals(PublicConstants.UniversalEnum.Video.getValue())){
                business = "video";
            }
            else {
                throw new Exception("文件/视频类型不明确");
            }

            List<String> uploadFileList = new ArrayList<>();
            for (MultipartFile multipartFile : fileList) {
                String uploadFile;
                try {
                    uploadFile = minioUtil.uploadFile(multipartFile, source, business);
                } catch (Exception e){
                    throw new Exception("文件上传失败");
                }

                uploadFileList.add(uploadFile);
            }
            return uploadFileList;
        }
        else {
            throw new Exception("没有接收到文件");
        }
    }

    /**
     * 模糊搜索运动处方
     * */
    @Override
    public List<Exerciseprescription> selectExerciseprescriptionByPrescriptionName(String prescriptionName) {
        List<Exerciseprescription> exerciseprescriptions = exerciseprescriptionDao.selectExerciseprescriptionByPrescriptionName(prescriptionName);
        if (exerciseprescriptions != null && exerciseprescriptions.size() > 0){
            for (Exerciseprescription exerciseprescription : exerciseprescriptions) {
                List<PublicVideo> publicVideos = publicVideoDao.selectPrescriptionvideo(exerciseprescription.getPrescriptionId());
                exerciseprescription.setPrescriptionVideo(publicVideos);
            }
            return exerciseprescriptions;
        }
        else {
            return null;
        }
    }

    /**
     * 条件查询康复运动
     * */
    @Override
    public List<ExerciseprescriptionDiseaseVo> selectExerciseprescriptionByDisease(Integer diseaseId, String people) throws Exception {
        List<Exerciseprescription> peoples = null;
        List<Exerciseprescription> diseaseIds = null;
        if (people != null && !people.equals("")){
            String peopleList = peopleList(people);
            peoples = exerciseprescriptionDao.selectExerciseprescriptionByPeople(peopleList);
        }

        if (diseaseId != null && diseaseId != 0){
            diseaseIds = exerciseprescriptionDao.selectExerciseprescriptionByDisease(diseaseId);
        }

        List<Exerciseprescription> exerciseprescriptionList = new ArrayList<>();
        if (peoples != null && peoples.size() > 0 && diseaseIds != null && diseaseIds.size() > 0){
            peoples.addAll(diseaseIds);

            List<Integer> idList = new ArrayList<>();
            for (Exerciseprescription exerciseprescription : peoples) {
                idList.add(exerciseprescription.getPrescriptionId());
            }

            List<Integer> ids = new ArrayList<>();
            for (Integer id : idList) {
                if (!ids.contains(id)) {
                    ids.add(id);
                }
            }

            for (Integer id : ids) {
                Exerciseprescription exerciseprescription = exerciseprescriptionDao.queryById(id);
                exerciseprescriptionList.add(exerciseprescription);
            }
        }
        else if (peoples != null && peoples.size() > 0){
            exerciseprescriptionList = peoples;
        }
        else if (diseaseIds != null && diseaseIds.size() > 0){
            exerciseprescriptionList = diseaseIds;
        }
        else {
            throw new Exception("未查到数据");
        }
        if (exerciseprescriptionList != null && exerciseprescriptionList.size() > 0){
            List<ExerciseprescriptionDiseaseVo> exerciseprescriptionDiseaseVoList = new ArrayList<>();
            for (Exerciseprescription exerciseprescription : exerciseprescriptionList) {
                ExerciseprescriptionDiseaseVo exerciseprescriptionDiseaseVo = new ExerciseprescriptionDiseaseVo();
                exerciseprescriptionDiseaseVo.setPrescriptionId(exerciseprescription.getPrescriptionId());
                exerciseprescriptionDiseaseVo.setPrescriptionName(exerciseprescription.getPrescriptionName());
                exerciseprescriptionDiseaseVo.setPrescriptionDesc(exerciseprescription.getPrescriptionDesc());
                if (exerciseprescription.getPeople() != null && !exerciseprescription.getPeople().equals("")){
                    exerciseprescriptionDiseaseVo.setPeople(suitableForTheCrowd(exerciseprescription.getPeople()));
                }
                exerciseprescriptionDiseaseVo.setExerciseGategory(exerciseprescription.getExerciseGategory());
                exerciseprescriptionDiseaseVo.setExerciseStrength(exerciseprescription.getExerciseLevel());
                exerciseprescriptionDiseaseVo.setExerciseTime(exerciseprescription.getExerciseTime());
                exerciseprescriptionDiseaseVo.setExerciseCourse(exerciseprescription.getExerciseCourse());
                exerciseprescriptionDiseaseVo.setDoctorPointout(exerciseprescription.getDoctorPointout());
                exerciseprescriptionDiseaseVo.setPointatPrescription(exerciseprescription.getPointatPrescription());
                exerciseprescriptionDiseaseVo.setOtherNote(exerciseprescription.getOtherNote());
                exerciseprescriptionDiseaseVo.setExercisePictureUrl(exerciseprescription.getExercisePictureUrl());
                exerciseprescriptionDiseaseVo.setPrice(exerciseprescription.getPrice());
                exerciseprescriptionDiseaseVo.setMemberVIP(exerciseprescription.getMemberVIP());
                exerciseprescriptionDiseaseVoList.add(exerciseprescriptionDiseaseVo);
            }
            return exerciseprescriptionDiseaseVoList;
        }
        else {
            throw new Exception("未曾有医生上传该类型运动处方");
        }
    }

    /**
     * 运动处方
     * */
    @Override
    public List<PrescriptionTypeVo> exercisePrescription(ExercisePrescriptionVo exercisePrescriptionVo) {
        if (exercisePrescriptionVo.getType() == null){
            exercisePrescriptionVo.setType("false");
        }

        List<PrescriptionTypeVo> prescriptionTypeVoList = new ArrayList<>();

        //获取全部运动处方
        List<Exerciseprescription> exerciseprescriptionList = exerciseprescriptionDao.exerciseprescriptionList(exercisePrescriptionVo);
        List<PatientExerciseprescription> patientExerciseprescriptionList = patientExerciseprescriptionDao.selectPatientCollect(exercisePrescriptionVo.getPatientId());
        if (exerciseprescriptionList != null && exerciseprescriptionList.size() > 0 &&
                patientExerciseprescriptionList != null){

            //我的运动处方
            PrescriptionTypeVo prescriptionTypeVo = new PrescriptionTypeVo();
            prescriptionTypeVo.setName("我的处方");
            //其他运动处方
            PrescriptionTypeVo typeVo = new PrescriptionTypeVo();
            typeVo.setName("其他处方");

            //我的
            List<PrescriptionVo> prescriptionVoList = new ArrayList<>();
            //其他
            List<PrescriptionVo> prescriptionVos = new ArrayList<>();
            for (Exerciseprescription exerciseprescription : exerciseprescriptionList) {
                for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptionList) {
                    if (exercisePrescriptionVo.getType().equals("true")) {//选中心脑血管疾病运动处方
                        if (exerciseprescription.getDiseaseType().equals("false")) {//不符合条件
                            continue;
                        }
                    }

                    PrescriptionVo prescriptionVo = new PrescriptionVo();
                    prescriptionVo.setExerciseprescriptionId(exerciseprescription.getPrescriptionId());
                    prescriptionVo.setUrl(exerciseprescription.getExercisePictureUrl());
                    prescriptionVo.setExerciseprescriptionName(exerciseprescription.getPrescriptionName());
                    prescriptionVo.setGrade(exerciseprescription.getExerciseLevel());
                    prescriptionVo.setPlan(exerciseprescription.getExerciseCourse().toString());
                    prescriptionVo.setDuration(exerciseprescription.getExerciseTime().toString());

                    Doctor doctor = doctorDao.selectDoctorById(exerciseprescription.getDoctorId());
                    if (doctor != null){
                        prescriptionVo.setDoctorName(doctor.getDoctorName());
                        prescriptionVo.setProfessional(doctor.getProfessional());
                    }

                    //我的
                    if (exerciseprescription.getPrescriptionId() == patientExerciseprescription.getPrescriptionId()){
                        prescriptionVoList.add(prescriptionVo);
                    }
                    //其他
                    else {
                        prescriptionVos.add(prescriptionVo);
                    }

                }
            }
            prescriptionTypeVo.setCount(prescriptionVoList.size());
            prescriptionTypeVo.setPrescriptionVoList(prescriptionVoList);

            typeVo.setCount(prescriptionVos.size());
            typeVo.setPrescriptionVoList(prescriptionVos);

            prescriptionTypeVoList.add(prescriptionTypeVo);
            prescriptionTypeVoList.add(typeVo);
        }
        else {
            //我的运动处方
            PrescriptionTypeVo prescriptionTypeVo = new PrescriptionTypeVo();
            prescriptionTypeVo.setName("我的处方");
            prescriptionTypeVo.setCount(0);
            //其他运动处方
            PrescriptionTypeVo typeVo = new PrescriptionTypeVo();
            typeVo.setName("其他处方");
            typeVo.setCount(0);
            prescriptionTypeVoList.add(prescriptionTypeVo);
            prescriptionTypeVoList.add(typeVo);
        }

        return prescriptionTypeVoList;
    }

    /**
     * 运动处方详情
     * */
    @Override
//    public Exerciseprescription exerciseDetails(Integer id, String idCard) throws Exception {
    public Exerciseprescription exerciseDetails(Integer id, Integer healthplanId) throws Exception {
//        Patient patient = patientDao.needBallCount(idCard);
//        if (patient != null){
//            throw new Exception("未查到患者信息");
//        }

        Exerciseprescription exerciseprescription = exerciseprescriptionDao.selectExerciseprescription(id, null);
        if (exerciseprescription != null){
            List<PublicVideo> publicVideos = publicVideoDao.selectPrescriptionvideo(id);
            if (publicVideos != null && publicVideos.size() > 0){
                exerciseprescription.setPrescriptionVideo(publicVideos);
            }

//            List<Healthplan> healthplans = healthplanDao.selectHealthplanByPatientId(patient.getPatientId());
//            if (healthplans != null &&healthplans.size() > 0){
//                for (Healthplan healthplan : healthplans) {
//                    HealthplanPrescription healthplanPrescription = healthplanPrescriptionDao.healthplanPrescriptionByid(exerciseprescription.getPrescriptionId(), healthplan.getHealthplanId());
//                    if (healthplanPrescription != null){
//                        ExerciserecordDao
//                    }
//                }
//            }
            exerciseprescription.setCurrentDay(2);
            exerciseprescription.setCurrentSportsNum(1);
        }
        exerciseprescription.setHealthplanId(healthplanId);
        return exerciseprescription;
    }

    /**
     * 患者的运动处方
     * */
    @Override
    public List<Exerciseprescription> patientExercise(Integer id) {
        List<Exerciseprescription> exerciseprescriptionList = new ArrayList<>();
        List<PatientExerciseprescription> patientExerciseprescriptionList = patientExerciseprescriptionDao.selectPatientCollect(id);
        if (patientExerciseprescriptionList != null && patientExerciseprescriptionList.size() > 0){
            for (PatientExerciseprescription patientExerciseprescription : patientExerciseprescriptionList) {
                Exerciseprescription exerciseprescription = exerciseprescriptionDao.queryById(patientExerciseprescription.getPrescriptionId());
                exerciseprescriptionList.add(exerciseprescription);
            }
        }
        return exerciseprescriptionList;
    }

    /**
     * 查询待修改的运动处方信息
     * */
    @Override
    public Exerciseprescription selectExercise(Integer id) {
        Exerciseprescription exerciseprescription = exerciseprescriptionDao.queryById(id);
        if (exerciseprescription.getClassificationId() != null && !exerciseprescription.getClassificationId().equals("")){
            String[] strArr= exerciseprescription.getClassificationId().split(",");
            if (strArr != null && strArr.length > 0){
                List<SysDict> sysDicts = sysDictDao.mainCategory();
                if (sysDicts != null && sysDicts.size() > 0){
                    List<SysDict> sysDictList = new ArrayList<>();
                    for (SysDict sysDict : sysDicts) {
                        List<SysDictItem> sysDictItemList = new ArrayList<>();
                        for (String s : strArr) {
                            Integer sid = Integer.parseInt(s);
                            SysDictItem sysDictItem = sysDictItemDao.queryById(sid);
                            if (sysDictItem != null && sysDict.getId() == sysDictItem.getDictId()){
                                sysDictItemList.add(sysDictItem);
                            }
                        }
                        sysDict.setSysDictItemList(sysDictItemList);

                        if (sysDict.getSysDictItemList() != null && sysDict.getSysDictItemList().size() > 0){
                            sysDictList.add(sysDict);
                        }
                    }
                    exerciseprescription.setSysDictList(sysDictList);
                }
            }
        }

        List<PrescriptionLabelOne> prescriptionLabelOnes = prescriptionLabelOneDao.prescriptionLabelOne(id);
        if (prescriptionLabelOnes != null && prescriptionLabelOnes.size() > 0){
            for (PrescriptionLabelOne prescriptionLabelOne : prescriptionLabelOnes) {
                List<PrescriptionLabelTwo> prescriptionLabelTwos = prescriptionLabelTwoDao.prescriptionLabelTwo(prescriptionLabelOne.getId());
                if (prescriptionLabelTwos != null && prescriptionLabelTwos.size() > 0){
                    for (PrescriptionLabelTwo prescriptionLabelTwo : prescriptionLabelTwos) {
                        List<PrescriptionLabelThree> prescriptionLabelThrees = prescriptionLabelThreeDao.prescriptionLabelThree(prescriptionLabelTwo.getId());
                        prescriptionLabelTwo.setLabelThrees(prescriptionLabelThrees);
                    }
                }
                prescriptionLabelOne.setLabelTwos(prescriptionLabelTwos);
            }
        }
        exerciseprescription.setLstLabel(prescriptionLabelOnes);
        return exerciseprescription;
    }

    /**
     * 条件查询康复运动健康和特殊考虑和环境考虑的人群枚举赋值
     * */
    public String peopleList(String people) throws Exception {
        if (people != null){
            switch (people){
                case "0"://怀孕
                    people = ("%" + PublicConstants.People.PREGNANT.getValue() + "%");
                    break;
                case "1"://青年
                    people = ("%" + PublicConstants.People.YOUTH.getValue() + "%");
                    break;
                case "2"://儿童
                    people = ("%" + PublicConstants.People.CHILD.getValue() + "%");
                    break;
                case "3"://老年人
                    people = ("%" + PublicConstants.People.ELDER.getValue() + "%");
                    break;
                case "4"://腰酸背痛
                    people = ("%" + PublicConstants.People.BACK_PAIN.getValue() + "%");
                    break;
                case "5"://环境考虑
                    people = ("%" + PublicConstants.People.ENVIRONMENT.getValue() + "%");
                    break;
                default:
                    throw new Exception("适用人群与环境参数出错了");
            }
        }
        return people;
    }

    /**
     * 条件查询康复运动健康和特殊考虑和环境考虑的人群枚举返回值处理
     * */
    public String suitableForTheCrowd(String people) throws Exception {
        String peopleList = "";
        if (people != null && !people.equals("")){
            List<String> list = Arrays.asList(people.split(","));
            for (int i = 0 ; i < list.size() ; i++) {
                String temporaryPeople = "";
                switch (list.get(i)){
                    case "elder":
                        temporaryPeople = "老人";
                        break;
                    case "youth":
                        temporaryPeople = "青年";
                        break;
                    case "child":
                        temporaryPeople = "儿童";
                        break;
                    case "pregnant":
                        temporaryPeople = "怀孕";
                        break;
                    case "backPain":
                        temporaryPeople = "腰酸背痛";
                        break;
                    case "environment":
                        temporaryPeople = "环境考虑";
                        break;
                    default:
                        throw new Exception("数据库适用人群枚举出错了");
                }

                if (i == 0){
                    peopleList = temporaryPeople;
                }
                else {
                    peopleList = peopleList + "," + temporaryPeople;
                }
            }
        }
        return peopleList;
    }
}